package com.jc.business.order;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jc.business.base.BaseBusiness;
import com.jc.cds.model.CustomXorder;
import com.jc.ds.base.service.BaseServiceException;
import com.jc.ds.model.OrderProducts;
import com.jc.ds.model.SupplierGoods;
import com.jc.ds.model.SupplierProduct;
import com.jc.framework.enums.Booleans;
import com.jc.service.model.jg.RequestAddOrder;
import com.jc.service.model.jg.RequestOrderGoods;
import com.jc.service.order.XorderService;
import com.jc.service.orderproduct.OrderProductsService;
import com.jc.service.supplier_product.SupplierProductService;
import com.jc.service.suppliergoods.SupplierGoodsService;


@Service
public class XorderBusiness extends BaseBusiness {
	
	@Autowired
	XorderService xorderService;
	
	@Autowired
	OrderProductsService  orderProductsService;
	
	@Autowired
	PushOrderBusiness pushOrderBusiness;
	
	@Autowired
	SupplierProductService supplierProductService;
	
	@Autowired
	SupplierGoodsService supplierGoodsService;
	/***
	 *
	 */
	@Transactional
	public void pushOrder() {
		//查出需要推送的订单, 条件,供应商配置为：允许自动推送订单
		List<CustomXorder> _list = xorderService.queryWaitPushOrder();
		for(CustomXorder _order : _list) {
			switch(_order.getSupplierName()) {
				case "吉贡云" :
					pushOrderBusiness.pushOrderToJG(packJgOrder(_order));
					break;
				case "其它"   :
					break;
			}
			
			
			
			
		}
	}
	
	public RequestAddOrder packJgOrder(CustomXorder co) {
		if(co == null) {
			return null;
		}
		RequestAddOrder rdOrder = new RequestAddOrder();
		if(StringUtils.isBlank(co.getConsigneeMobile())) {
			throw new BaseServiceException("无法封装吉贡订单报文,缺少收货人手机号");
		}
		rdOrder.setMobile(co.getConsigneeMobile());
		rdOrder.setAddress(co.getConsigneeAddress());
		rdOrder.setArea(co.getConsigneeRegionName());
		rdOrder.setProvince(co.getConsigneeProvinceName());
		//发票类型暂时传0, 1.个人2.公司 ，暂不支持开发票
		rdOrder.setBillType(0);
		rdOrder.setIsBill(0);   //是否开发票
		rdOrder.setCity(co.getConsigneeCityName());
		rdOrder.setLinkMan(co.getConsigneeName());
		rdOrder.setSelfOrderId(co.getId());
		if(StringUtils.equals(co.getIsOverseas(), Booleans.TRUE.getName())) {
			if(StringUtils.isBlank(co.getConsigneeCardCode())) {
				throw new BaseServiceException("该条订单包含海外购产品, 缺少实名身份证号");
			}
			rdOrder.setIdNumber(co.getConsigneeCardCode());
		}
		if(StringUtils.isNotBlank(co.getBuyerMessage())) {
			rdOrder.setRemark(co.getBuyerMessage());
		}
		
		OrderProducts _op = new OrderProducts();
		_op.setOrderId(co.getId());
		_op.setIsBind(0);
		List<OrderProducts> queryAll = orderProductsService.queryAll(_op);
		List<RequestOrderGoods> orderDetail = new ArrayList<RequestOrderGoods>();
		RequestOrderGoods _p;
		for(OrderProducts p : queryAll) {
			_p = new RequestOrderGoods();
			_p.setAppOrderSn(co.getChannelOrderId());
			SupplierProduct spu = supplierProductService.queryById(Long.parseLong(p.getSupplierSkuId()));
			if(spu == null) {
				throw new BaseServiceException("严重错误: " + p.getSupplierProductId() + "该product id 在渠道SKU表中没有维护");
			}
			_p.setBrandId(0);
			_p.setChannel(1);//电商
			 //* 配送方式
			 // 1.及时送2.到店取3.快递送
			_p.setDeliveryWay(3);
			SupplierGoods goods = supplierGoodsService.queryById(spu.getGid());
			_p.setGoodId(Long.valueOf(goods.getSupplierGoodsId()));
			_p.setGoodName(p.getProductName());
			_p.setInventoryId(Long.valueOf(spu.getInventoryId()));
			_p.setNavigateImg1("");
			_p.setProductId(Long.valueOf(spu.getProductId()));
			_p.setProductPrice(0);
			_p.setQuantity(p.getProductNums());
			_p.setSalePrice(0);
			_p.setStoreId(Long.valueOf(spu.getStoreId()));
			
			orderDetail.add(_p);
		}
		rdOrder.setOrderDetailList(orderDetail);
		return rdOrder;
		
	}
	
	
	
	
	
	
	

	

}
